package com.chainyoung.component_tmperson.ui.activity.hdwallet;

import android.content.Intent;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.BlurBuilder;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.ui.adapter.WordsAdapter;
import com.chainyoung.component_tmperson.ui.view.WordsRemindDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

//助记词
public class WordsActivity extends BaseActivity {

    @BindView(R2.id.grid_view)
    GridView gridView;
    @BindView(R2.id.txt1)
    TextView txt1;
    @BindView(R2.id.tv_save)
    TextView tvSave;


    private boolean isFirstLook;//第一次备份
   // private HdWallets hdWallets;
    private List<String> words = new ArrayList<>();

    private WordsAdapter wordsAdapter;

    private HdWalletCache hdWallet;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected void initUI() {
        isFirstLook = getIntent().getBooleanExtra("isFirst", false);
        hdWallet= (HdWalletCache) getIntent().getSerializableExtra("hdwallet");
        wordsAdapter = new WordsAdapter(mContext, words);
        gridView.setAdapter(wordsAdapter);
        setCurrentTitle(isFirstLook ? "抄写助记词" : "查看助记词");

        if (!isFirstLook) {
            txt1.setText(getString(R.string.tmperson_backup_words4));
            tvSave.setText(R.string.tmperson_look_up_crypt_qr_code);
        }


    }

    private WordsRemindDialog wordsRemindDialog;

    private void showRemindDiaLog() {
        wordsRemindDialog = new WordsRemindDialog(mContext, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadWords();
                wordsRemindDialog.close();
            }
        });
        wordsRemindDialog.show();
    }


    private void loadWords() {
        /*hdWallets = WalletStoreManger.getHdWallets();
        if (hdWallets != null) {
            words.addAll(Arrays.asList(hdWallets.getMnemonic().split(" ")));
            wordsAdapter.notifyDataSetChanged();
        }*/
        if (hdWallet!=null){
            words.addAll(Arrays.asList(hdWallet.getMnemonic().split(" ")));
            wordsAdapter.notifyDataSetChanged();
        }

    }


    @Override
    protected void initData() {
        if (isFirstLook) {
            showRemindDiaLog();
        } else {
            loadWords();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_check_words;
    }

    @OnClick(R2.id.tv_save)
    void save() {
        if (isFirstLook) {
            Intent intent = new Intent(mContext, ConfirmSaveWordsActivity.class);
            intent.putExtra("step", 1);
            intent.putExtra("hdWallet",hdWallet);
            mContext.startActivity(intent);
            finish();
        } else {
            //查看加密二维码
            BlurBuilder.snapShotWithoutStatusBar(this);
            Intent intent = new Intent(mContext, WordsCodeActivity.class);
            intent.putExtra("hdWallet", hdWallet);
            startActivity(intent);
            mContext.overridePendingTransition(
                    android.view.animation.Animation.INFINITE,
                    android.view.animation.Animation.INFINITE);
        }
    }


}
